import java.util.HashMap;
/**
 * This class allows you to create a user-account, to log-in with that account and to delete it
 * 
 * @author Mathieu Reymond & Jonathan Smet
 * @version 03/05/11
 */
public abstract class Account
{
    /**
     * Let you create a user, under certain conditions : the email and pseudonym of the new user have to be unique, no other users may have the same email/pseudonym
     * 
     * @param userName The pseudonym of the user
     * @param userEmail the user's email
     * @param password the user's password
     * @param year The user's year birth
     * @param month The month of the user's birthday
     * @param day The day of the user's birthday
     * @param boolean gender The user's gender : true if he is a male, false otherwise
     * @param firstName The user's first name
     * @param lastName The user's last name
     * 
     * @return True if the account was succesfully created, false if not
     */
    public static boolean createUser(String userName, String userEmail, String password, int year, int month, int day, boolean gender, String firstName, String lastName)
    {
        boolean result = false;
        
        if(userCheck(userName) || emailCheck(userEmail))
        {
            result = true;
        }
        
        if(!result)
        {
            UserData.addUser(new User(userName, userEmail, password, year, month, day, gender, firstName, lastName));
        }
        return (!result);
    }
    
    /**
     * Delete a user from the user-database
     * 
     * @param name The pseudonym of the user's that's going to be deleted
     */
    public static void deleteAccount(String name)
    {
        UserData.deleteUser(name);
    }
    
    /**
     * Loggin with an email and password
     * 
     * @param email The user's email
     * @param password The user's password
     * 
     * @return The user that logged in or null if error (password incorrect, email doesn't exist)
     */
    public static User loggin(String email, String password)
    {
        User result = null;
        
        if(emailCheck(email))
        {
            String nameList[] = UserData.getNameList();
            HashMap<String, User> users = UserData.getUserData();
            for(int index = 0; index < nameList.length; index++)
            {
                User check = users.get(nameList[index]);
                if(check.getEmail().equalsIgnoreCase(email) && check.getPassword().equals(password))
                {
                    result = check;
                }
            }
        }
        return result;
    }
    
    /**
     * Let you know if a specific pseudonym already exists or not
     * 
     * @param userName The name to check
     * 
     * @return True if it already exists, false otherwise
     */
    public static boolean userCheck(String userName)
    {
        String nameList[] = UserData.getNameList();
        boolean result = false;
        for(int index = 0; index < nameList.length; index++)
        {
            if(nameList[index].equalsIgnoreCase(userName))
            {
                result = true;
            }
        }
        return result;
    }
    
    /**
     * Let you know if a specific email already exists or not
     * 
     * @param emailToCheck The email to check
     * 
     * @return True if it already exists, false otherwise
     */
    public static boolean emailCheck(String emailToCheck)
    {
        String nameList[] = UserData.getNameList();
        boolean result = false;
        HashMap<String,User> users = UserData.getUserData(); 
        for(int index =0; index < nameList.length; index++)
        {
            if(users.get(nameList[index]).getEmail().equalsIgnoreCase(emailToCheck))
            {
                result = true;
            }
        }
        return result;
    }
}
